package cn.stylefeng.guns.modular.robot.controller;

import cn.stylefeng.guns.core.common.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.robot.entity.GroupRelationship;
import cn.stylefeng.guns.modular.robot.entity.vo.req.GroupListReq;
import cn.stylefeng.guns.modular.robot.service.GroupRelationshipService;
import cn.stylefeng.guns.modular.robot.service.GroupService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/group")
public class GroupController extends BaseController {

    private static String PREFIX = "/modular/system/group/";
    @Resource
    private GroupService groupService;
    @Resource
    private GroupRelationshipService groupRelationshipService;

    @RequestMapping("")
    public String index() {
        return PREFIX + "group.html";
    }

    @RequestMapping("/add")
    public String addView() {
        return PREFIX + "group_add.html";
    }

    @RequestMapping("/group_edit")
    public String editView() {
        return PREFIX + "group_edit.html";
    }

    @PostMapping("/addGroup")
    @ResponseBody
    public ResponseData add(@RequestBody GroupListReq groupListReq) {
        groupService.addGroup(groupListReq);
        return SUCCESS_TIP;
    }

    @PostMapping("/editGroup")
    @ResponseBody
    public ResponseData edit(@RequestBody GroupListReq groupListReq) {
        groupService.edit(groupListReq);
        return SUCCESS_TIP;
    }

    @GetMapping("/bindRelationship")
    public String editbindRelationship() {
        return PREFIX + "bindRelationship.html";
    }

    @GetMapping("/list")
    @ResponseBody
    public LayuiPageInfo list(@RequestParam(value = "tgGroupId", required = false) Long tgGroupId, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "userName", required = false) String userName, @RequestParam(value = "limit", required = false) Long pageSize, @RequestParam(value = "page", required = false) Long pageNum) {
        return groupService.list(tgGroupId, name, userName, pageSize, pageNum);
    }

    @PostMapping("/detail")
    @ResponseBody
    public ResponseData detail(@RequestParam(value = "id") Long id) {
        return groupService.detail(id);
    }

    @PostMapping("/bindRelationship")
    @ResponseBody
    public ResponseData bindRelationship(GroupRelationship groupRelationship) {
        groupRelationshipService.bindRelationship(groupRelationship);
        return SUCCESS_TIP;
    }

    @PostMapping("/unbindRelationship")
    @ResponseBody
    public ResponseData unbindRelationship(@RequestParam("sendGroupId") Long sendGroupId) {
        groupRelationshipService.unbindRelationship(sendGroupId);
        return SUCCESS_TIP;
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseData delete(@RequestParam("id") Long id){
        groupService.delete(id);
        return SUCCESS_TIP;
    }
}
